<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.yuxin.common.util.DBUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="java.util.regex.Matcher"%>
<%@page import="com.yuxin.core.TransactionManager"%>
<%
	Connection conn = null;
	// Init trader list
	List<Map<String, Object>> traderList = null;
	TransactionManager tranM = null;
	String redirect = null;
	try{
		conn = DBUtil.getConnection();
		String sql = "select t.ID, t.NAME, t.LICENSE_IMG, t.CONTACT, t.PHONE, t.STATUS, DATE_FORMAT(t.CREATE_TIME, '%Y-%m-%d %H:%i') as CREATETIME from p_trade t where t.STATUS <> ?";
		traderList = DBUtil.queryList(conn, sql, "1");
		
		// Change trader status
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		
		if(id != null && !"".equals(id) && status != null && !"".equals(status)) {
			tranM = new TransactionManager(conn);
			tranM.begin();
			Pattern p = Pattern.compile("[2,3]");
			Matcher m = p.matcher(status);
			
			String errMsg = "<div align='center'><div style='border:4px solid #F00; line-height:100px; width:300px; text-align:center; margin-top:100px;'><strong>[msg]</strong></div></div>";
			if(!m.matches()) {
				out.println(errMsg.replace("[msg]", "错误的状态！"));
				return;
			}
			sql = "update p_trade set STATUS = ? where ID = ?";
			int res = DBUtil.executeSql(conn, sql, status, id);			
			if(res > 0) {
				redirect = request.getContextPath()+"/admin/trader_management.jsp";				
			}
		}
	} catch (Exception ex) {
		if(tranM != null) {
			tranM.rollback();
		}
		ex.printStackTrace();
	} finally {		
		if(tranM != null) {
			tranM.commit();
		}
		DBUtil.close(conn);
		if(redirect != null) {
			response.sendRedirect(redirect);
		}
	}
%>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/meta.jsp" %>
    <title>${site_title}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
	<!--
		#prod_list td {text-align:center; vertical-align:middle;}
	-->
	</style>
</head>
<body>
<%@ include file="/header.jsp" %>
<div class="container" >
    <div class="row-fluid">
        <%@ include file="menu.jsp" %>
        <div class="span9">        	
           <table id="prod_list" class="table table-bordered" style="border: 1px solid #CCC; margin-top:20px;">
                <thead style="background-color: #CCCCCC;">
                    <tr>
                        <td width="180px">营业执照</td>
                        <td width="100px">负责人</td>
                        <td width="120px">联系电话</td>
                        <td width="80px">状态</td>
                        <td></td>
                    </tr>
                </thead>    
                <tbody>
                    <%
						if(traderList == null || traderList.size() == 0) {
							out.println("<tr><td  colspan='5'>暂无记录！</td></tr>");	
						}
                        for(Map<String, Object> map : traderList) {
                    %>
                    <tr>
                        <td>
                        	<img src="<%=request.getContextPath() %>/<%=map.get("LICENSE_IMG") %>" width="60" height="60" style="vertical-align:middle" />&nbsp;&nbsp;
							<span><%=map.get("NAME") %></span>
                        </td>	
                        <td><%=map.get("CONTACT") %></td>
                        <td><%=map.get("PHONE") %></td>
                        <td><%="1".equals(map.get("STATUS")) ? "待审核" : "2".equals(map.get("STATUS")) ? "营业" : "停业" %></td>
                        <td align="center">                        
                        	<div><a href="${ctx}/site/index_dp.jsp?tradeId=<%=map.get("ID") %>" target="_blank">查看店铺</a></div>
                            <div style="margin-top:10px;"><a href="trader_detail.jsp?id=<%=map.get("ID") %>">查看详情</a></div>
                            <div style="margin-top:10px;">
                            	<a href="trader_management.jsp?id=<%=map.get("ID") %>&amp;status=<%="2".equals(map.get("STATUS")) ? "3" : "2" %>">
									<%="2".equals(map.get("STATUS")) ? "强制停业" : "重新营业" %>
                                </a>
                            </div>
                         </td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
$(document).ready(function(){
	$("#menu_trader_management").addClass("active");
});
</script>
<%@ include file="/footer.jsp" %>
</body>
</html>